package com.loan.psychicloan.fragment;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.loan.psychicloan.R;
import com.loan.psychicloan.activity.AboutUsActivity;
import com.loan.psychicloan.activity.FeedBackActivity;
import com.loan.psychicloan.activity.CusmoterActivity;
import com.loan.psychicloan.activity.LoginActivity;
import com.loan.psychicloan.activity.MainActivity;
import com.orhanobut.logger.Logger;


public class FragmentMine extends Fragment implements View.OnClickListener{

    Toolbar mtoolBar;
    ImageView iv_login_next;
    TextView tv_login;
    ImageView iv_customer_next;
    ImageView iv_about_next;
    ImageView iv_feedback_next;
    Button btn_loginout;

    public static FragmentMine newInstance(){
        FragmentMine fragmentMine = new FragmentMine();
        Bundle bundle=new Bundle();
        bundle.putString("text", "我的");
        fragmentMine.setArguments(bundle);
        return fragmentMine;
    }
    @Nullable @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_mine,container,false);
        TextView textView =  view.findViewById(R.id.title_toolbar_titletext);
        Log.d("xyz", "onCreateView: "+getArguments().getString("text"));
        textView.setText(getArguments().getString("text"));
        mtoolBar = view.findViewById(R.id.main_toolbar);
        iv_login_next = view.findViewById(R.id.iv_mine_next);
        tv_login = view.findViewById(R.id.tv_mine_login);
        iv_customer_next = view.findViewById(R.id.iv_customer_next);
        iv_about_next = view.findViewById(R.id.iv_about_next);
        iv_feedback_next = view.findViewById(R.id.iv_feedback_next);
        btn_loginout = view.findViewById(R.id.loginout);

        setTvLogin();

        initclick();

        return view;
    }

    private void setTvLogin() {
        if (!MainActivity.LOGIN){
            btn_loginout.setVisibility(View.GONE);
            tv_login.setText(getText(R.string.tv_mine_login));
        }else{
            btn_loginout.setVisibility(View.VISIBLE);
            tv_login.setText(MainActivity.PHONE_NUMBER);
        }
    }

    private void initclick() {
        iv_login_next.setOnClickListener(this);
        iv_customer_next.setOnClickListener(this);
        iv_about_next.setOnClickListener(this);
        iv_feedback_next.setOnClickListener(this);
        btn_loginout.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        Intent mIntent = null;

        switch (view.getId()){

            case R.id.iv_mine_next:
                mIntent = new Intent(getActivity(),LoginActivity.class);
                break;
            case R.id.iv_customer_next:
                mIntent = new Intent(getActivity(), CusmoterActivity.class);
                break;
            case R.id.iv_feedback_next:
                mIntent = new Intent(getActivity(), FeedBackActivity.class);
                break;
            case R.id.iv_about_next:
                mIntent = new Intent(getActivity(), AboutUsActivity.class);
                break;
            case  R.id.loginout:
                SharedPreferences sp = getActivity().getSharedPreferences("config",0);
                SharedPreferences.Editor editor = sp.edit();
                editor.clear();
                editor.apply();

                MainActivity.LOGIN =false;
                MainActivity.PHONE_NUMBER =null;
                setTvLogin();
                //mIntent = new Intent(getActivity(),MainActivity.class);

                break;
                default:
                    break;
        }
        if (view.getId() != R.id.loginout){
            startActivity(mIntent);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        setTvLogin();
        Logger.t("fragmentMine.onResume");
    }
}

